﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using KEW.InterFace;

namespace AutoGam
{
    public partial class SalesPswd : Form
    {
        private const int QtyLength = 3;
        private const int NumberLen = 7;
        private const int CarLength = 7;
        private Boolean isAutoMax = false;
        private String stamp;
        private const Boolean isMustUseCard = false;
        public string CardSerial = "";
        public string CardNo = "";
        public string BaseName = "";
        private int TrendsId = 0;
        
        public String PreItem = String.Empty;
        String OrgName = "";

        public Boolean isPswdInputFocus
        {
            get { return isPswdInputFocusValue; }
            set 
            {
                if (value)
                {
                    PageSales_PswdText.BackColor = Color.Black;
                    PageSales_NumText.BackColor = Color.Transparent;
                }
                else
                {
                    PageSales_PswdText.BackColor = Color.Transparent;
                    PageSales_NumText.BackColor = Color.Black;
                }

                isPswdInputFocusValue = value;
            }
        }

        private Boolean isPswdInputFocusValue;

        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= 0x02000000;
                return cp;
            }
        }

        public SalesPswd()
        {
            InitializeComponent();
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景.
            SetStyle(ControlStyles.DoubleBuffer, true); // 双缓冲

            
            if (isMustUseCard)
            {
                label9.Text = "请先刷提货卡";
                isPswdInputFocus = true;
            }
            else
            {
                label9.Text = "请先刷提货卡或输入订单编号";
                isPswdInputFocus = false;
            }

            if (!Program.IsDebug)
            {
                this.TopMost = true;
                this.WindowState = FormWindowState.Maximized;
            }
        }



        private void PageSales_NumClear_Click(object sender, EventArgs e)
        {
            Device.ButtonVoice();
            if (isPswdInputFocus)
            {
                PageSales_PswdText.Text = String.Empty;
            }
            else
            {
                PageSales_NumText.Text = String.Empty;
            }
            
        }

        private void PageSales_NumDelete_Click(object sender, EventArgs e)
        {
            Device.ButtonVoice();
            if (isPswdInputFocus)
            {
                if (PageSales_PswdText.Text.Length > 0)
                {
                    PageSales_PswdText.Text = PageSales_PswdText.Text.Substring(0, PageSales_PswdText.Text.Length - 1);
                }
            }
            else
            {
                if (PageSales_NumText.Text.Length > 0)
                {
                    PageSales_NumText.Text = PageSales_NumText.Text.Substring(0, PageSales_NumText.Text.Length - 1);
                }
            }
        }

        private void PageSales_Num_Click(object sender, EventArgs e)
        {
            Device.ButtonVoice();

            if (isPswdInputFocus)
            {
                if (PageSales_PswdText.Text.Length < 6)
                {
                    PageSales_PswdText.Text += ((Button)sender).Text;
                }
            }
            else
            {
                if (PageSales_NumText.Text.Length < NumberLen)
                {
                    PageSales_NumText.Text += ((Button)sender).Text;
                }
            }
        }

        private void PageSales_NumNext_Click(object sender, EventArgs e)
        {
            ButtonStart();
            if (PageSales_NumText.Text.Length == NumberLen)
            {
                Boolean isPass = false;
                
                String baseKey = PageSales_NumText.Text.Substring(0, 1);
                String billNo = PageSales_NumText.Text.Substring(1, 6);
                String accPwd = PageSales_PswdText.Text;
                TrendsId = 0;


                if (Program.DataConnString.ContainsKey(baseKey))
                {
                    
                    OleDbConnection conn = new OleDbConnection();
                    conn.ConnectionString = Program.DataConnString[baseKey];

                    try
                    {
                        PageSales_NumMessage.Text = "正在查询，请稍候...";
                        Device.Voice("voice1.wav");
                        Application.DoEvents();
                        DateTime now = DateTime.Now;
                        String sql = String.Format("SELECT A.FName,A.FID,Isnull(B.FDecimal2,0) BagQty FROM KEW_ProductsEntry A LEFT JOIN KEW_ProductsBagRuleEntry B ON A.FBase1=B.FID\r\n");
                        sql += String.Format("SELECT A.FID,A.FID_SRC,A.FComboBox PwdType,A.FText2 FixPwd,B.FName OrgName FROM KEW_SalesAccount A LEFT JOIN t_Organization B ON A.FBase1=B.FItemID WHERE A.FBillNo='{0}' AND A.FUser>0 AND A.FCheckBox1=0\r\n", billNo);
                        sql += String.Format("SELECT FValue FROM t_systemprofile WHERE fcategory='general' and fkey='CompanyName'");

                        OleDbDataAdapter adapter = new OleDbDataAdapter(sql, conn);
                        DataSet data = new DataSet();
                        adapter.Fill(data);
                        String companyName = data.Tables[2].Rows[0]["FValue"].ToString();
                        companyName = companyName.Substring(0, companyName.Length > 8 ? 8 : companyName.Length);
                        if (BaseName != "" && BaseName != companyName)
                        {
                            PageSales_NumMessage.Text = String.Format("该磁卡不是本厂发出，请检查。");
                        }
                        else if (data.Tables[0].Rows.Count > 0 && data.Tables[1].Rows.Count == 1)
                        {
                            DataRow AccRow = data.Tables[1].Rows[0];
                            OrgName = AccRow["OrgName"].ToString();
                            switch (AccRow["PwdType"].ToString())
                            {
                                case "0":

                                    break;
                                case "1":
                                    if (AccRow["FixPwd"].ToString() != accPwd)
                                    {
                                        PageSales_NumMessage.Text = "错误的密码";
                                        Device.Voice("密码错误.wav");
                                        ButtonEnd();
                                        return;
                                    }
                                    break;
                                case "2":
                                    sql = String.Format("SELECT TOP 1 A.FClassID_SRC BingType,B.FEntryID FROM KEW_PassWord A LEFT JOIN KEW_PassWordEntry B ON A.FID=B.FID\r\n");
                                    sql += String.Format("WHERE (A.FClassID_SRC=236900147 AND FID_SRC={0}) OR (A.FClassID_SRC=236900152 AND FID_SRC={1}) AND B.FText='{3}' AND B.FCheckBox=0 ORDER BY B.FIndex", AccRow["FID"].ToString(), AccRow["FID_SRC"].ToString(), AccRow["accPwd"].ToString());
                                    OleDbDataAdapter adapter_pwd = new OleDbDataAdapter(sql, conn);
                                    DataSet data_pwd = new DataSet();
                                    adapter.Fill(data_pwd);

                                    if (data_pwd.Tables[0].Rows.Count == 1)
                                    {
                                        TrendsId = Common.CInt(data_pwd.Tables[0].Rows[0].ToString());
                                    }
                                    else
                                    {
                                        PageSales_NumMessage.Text = "错误的密码";
                                        Device.Voice("密码错误.wav");
                                        ButtonEnd();
                                        return;
                                    }

                                    break;
                            }

                            isPass = true;
                            stamp = Guid.NewGuid().ToString();
                            DataRow row = data.Tables[1].Rows[0];
                            OrderID = row["FID"].ToString();

                            PageSales_ItemList.DisplayMember = "Name";
                            PageSales_ItemList.ValueMember = "ID";
                            PageSales_ItemList.Items.Clear();

                            foreach (DataRow list in data.Tables[0].Rows)
                            {
                                ListTemp temp = new ListTemp();
                                temp.ID = list["FID"].ToString();
                                temp.Name = list["FName"].ToString();
                                PageSales_ItemList.Items.Add(temp);

                                if (PreItem == temp.Name)
                                {
                                    PageSales_ItemList.SelectedItem = temp;
                                }
                            }

                            PageCar_Panel.Enabled = (PageCar_Text.Text == "");
                            PageSales_ItemList.Enabled = (PageSales_ItemList.SelectedItem == null);
                            PageSales_QtyPanel.Enabled = true;
                            SalesTab.SelectedTab = TabPageInuputCarNo;
                        }
                        else
                        {
                            PageSales_NumMessage.Text = "错误的编号(同时有可能是订单尚未审核，请检查确认)";
                            Device.Voice("编号不正确.wav");
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                    if (conn.State == ConnectionState.Open)
                    {
                        conn.Close();
                    }
                    conn.Dispose();
                }
                else
                {
                    PageSales_NumMessage.Text = "错误的编号。";
                    Device.Voice("编号不正确.wav");
                }
            }
            else
            {
                PageSales_NumMessage.Text = String.Format("您输入的编号长度不是{0}位。", NumberLen);
                Device.Voice("编号不正确.wav");
            }
            ButtonEnd();
        }

        private void PageCar_Next_Click(object sender, EventArgs e)
        {
            ButtonStart();
            if (PageCar_Text.Text != String.Empty)
            {
                SalesTab.SelectedTab = TabPageSalesInuputPreQty;
            }
            //if (PageSales_PreQty.Text == "" || PageSales_ItemList.SelectedItem == null)
            //{
            //    SalesTab.SelectedTab = TabPageSalesInuputPreQty;
            //    PageSales_ItemList.Enabled = (PageSales_ItemList.SelectedItem == null);
            //    PageSales_QtyPanel.Enabled = (PageSales_PreQty.Text == "");
            //}
            //else
            //{
            //    SalesTab.SelectedTab = TabPageSalesConfirm;
            //}
            ButtonEnd();
        }

        private void PageSales_QtyNext_Click(object sender, EventArgs e)
        {
            ButtonStart();
            if (PageSales_ItemList.SelectedItem == null)
            {
                PageSales_PreMessage.Text = "请选择一个产品！";
            }
            else if (Common.CDec(PageSales_PreQty.Text) == 0)
            {
                PageSales_PreMessage.Text = "请输入预装量！";
            }
            else
            {
                SalesTab.SelectedTab = TabPageSalesConfirm;
            }
            ButtonEnd();
        }

        DateTime ButtonOverTime = DateTime.Now;
        Boolean IsButtonEnable = true;

        private void ButtonStart()
        {
            Device.ButtonVoice();
            
            PageSales_NumNext.Enabled = false;
            PageSales_NumCancel.Enabled = false;
            PageCar_Next.Enabled = false;
            PageCar_Cancel.Enabled = false;
            PageSales_QtyNext.Enabled = false;
            PageSales_QtyCancel.Enabled = false;
            PageConfirm_Next.Enabled = false;
        }

        private void ButtonEnd()
        {
            ButtonOverTime = DateTime.Now;
            IsButtonEnable = false;
        }

        private void PageConfirm_Next_Click(object sender, EventArgs e)
        {
            ButtonStart();
            PageSales_NumNext.Enabled = false;
            PageConfirm_Message.Text = "请稍候，正在为您办理！";
            PageConfirm_Next.Text = "请 稍 候";
            Device.Voice("voice1.wav");
            PageConfirm_Next.Enabled = false;
            String itemID = ((ListTemp)PageSales_ItemList.SelectedItem).ID;

            Application.DoEvents();

            DataSet data = new DataSet();
            OleDbConnection conn = new OleDbConnection();
            conn.ConnectionString = Program.DataConnString[PageSales_NumText.Text.Substring(0, 1)];
            Decimal Qty = Common.Qty(PageSales_PreQty.Text);
            Decimal SumQty=0;
            Decimal SumBalQty = 0;
            Boolean IsInBag;
            String[] GustName;

            StringBuilder sql = new StringBuilder();
            sql.Append("EXEC KEW_SALES_CHECKREADJUST\r\n");
            sql.Append("SELECT E.FDecimal5 AmountBal,IsNull(D.FDecimal3,IsNull(P.FDecimal2,0)) FItemPrice,IsNull(D.FText,'挂牌价') FSource,P.FCheckBox IsInBag,G.FName GName \r\n");
            sql.Append("FROM KEW_SalesAccount E \r\n");
            sql.AppendFormat("LEFT JOIN KEW_SalesAccountEntry D ON D.FID=E.FID AND D.FBase2='{0}'\r\n", itemID);
            sql.AppendFormat("LEFT JOIN KEW_ProductsEntry P ON P.FID='{0}'\r\n", itemID);
            sql.AppendFormat("LEFT JOIN t_Organization G ON G.FItemID=E.FBase\r\n");
            sql.AppendFormat("WHERE E.FID='{0}' AND E.FCheckBox1=0\r\n", OrderID);
            sql.AppendFormat("SELECT 1 FROM KEW_SalesInfo WHERE FText='{0}' AND FComboBox IN ('0','1','2') AND FCheckBox4=0", PageConfirmCar.Text);

            OleDbDataAdapter adapter = new OleDbDataAdapter(sql.ToString(), conn);
            
            AccountList.Clear();

            try
            {
                adapter.Fill(data);

                if (data.Tables[1].Rows.Count > 0)
                {
                    PageConfirm_Message.Text = "车辆" + PageConfirmCar.Text + "尚未出厂，不能重复开卡。";
                    PageConfirm_Next.Text = "禁止";
                    PageConfirm_Next.Enabled = true;
                    return;
                }

                IsInBag = Common.CBool(data.Tables[0].Rows[0]["IsInBag"].ToString());
                GustName = Com.Split(data.Tables[0].Rows[0]["GName"].ToString());
                foreach (DataRow row in data.Tables[0].Rows)
                {

                    AccountTemp temp = new AccountTemp();
                    //temp.ID = row["EntryID"].ToString();
                    temp.AmountBal = Common.Amount(row["AmountBal"].ToString());
                    temp.ItemPrice = Common.Price(row["FItemPrice"].ToString());
                    temp.SumPrice = temp.ItemPrice;
                    if (temp.SumPrice <= 0)
                    {
                        PageConfirm_Message.Text = "产品单价出现错误，请联系管理员维护！";
                        PageConfirm_Next.Enabled = true;
                        PageConfirm_Next.Text = "确    定";
                        ButtonEnd();
                        return;
                    }
                    temp.QtyBal = Common.CutDecimal(temp.AmountBal/temp.SumPrice, 2);
                    temp.Source = row["FSource"].ToString();

                    SumBalQty += temp.QtyBal;
                    if (Qty > temp.QtyBal)
                    {
                        temp.Qty = temp.QtyBal;
                        Qty = Qty - temp.QtyBal;
                    }
                    else
                    {
                        temp.Qty = Qty;
                        Qty = 0;
                    }
                    temp.Amount = Common.Amount((temp.Qty * temp.SumPrice).ToString());
                    SumQty += temp.Qty;
                    AccountList.Add(temp);
                }
                if (SumQty != Common.Qty(PageSales_PreQty.Text))
                {
                    PageConfirm_Message.Text = String.Format("订单余额不足，请补交充值或者调整预装数量重试！剩余:{0}吨", SumBalQty);
                    PageConfirm_Next.Enabled = true;
                    Device.Voice("余额不足.wav");
                    PageConfirm_Next.Text = "确    定";
                    ButtonEnd();
                    return;
                }
                String serial = Device.GetCardAndReturnSerial();
                if (serial == String.Empty)
                {
                    PageConfirm_Message.Text = "磁卡用光了，请联系管理人员！";
                    PageConfirm_Next.Enabled = true;
                    PageConfirm_Next.Text = "确    定";
                }
                else
                {
                    int tryTimes = 0;
                    int canTimes = 5;
                    //while (tryTimes < canTimes)
                    //{
                    //    if (Device.UsbCardDevice.WriteBlock(GustName[0], Block.Block04) &&
                    //    Device.UsbCardDevice.WriteBlock(GustName[1], Block.Block05) &&
                    //    Device.UsbCardDevice.WriteBlock(GustName[2], Block.Block06) &&
                    //    Device.UsbCardDevice.WriteBlock(PageConfirmItem.Text, Block.Block08) &&
                    //    Device.UsbCardDevice.WriteBlock(PageConfirmCar.Text, Block.Block09) &&
                    //    Device.UsbCardDevice.WriteBlock("", Block.Block10) &&  //备注
                    //    Device.UsbCardDevice.WriteBlock("", Block.Block12) && //收发人
                    //    Device.UsbCardDevice.WriteBlock(PageConfirmQty.Text, Block.Block13) &&
                    //    Device.UsbCardDevice.WriteBlock("正常", Block.Block14) &&
                    //    Device.UsbCardDevice.WriteBlock("销售.已开票", Block.Block16) &&
                    //    Device.UsbCardDevice.WriteBlock("", Block.Block17) && //销售批次编号
                    //    Device.UsbCardDevice.WriteBlock("", Block.Block18)) //生产线
                    //    {
                    //        break;
                    //    }
                    //    else
                    //    {
                    //        tryTimes++;
                    //    }
                    //}
                    if (tryTimes < canTimes)
                    {
                        sql.Clear();
                        sql.AppendFormat("EXEC KEW_SALES_AUTOCARD1 '{0}','{1}','{2}',{3},'{4}',{5},'{6}','{7}({8})'", OrderID, serial, PageConfirmCar.Text, PageConfirmQty.Text, itemID, TrendsId, stamp, CardSerial, CardNo);
                        try
                        {
                            data = new DataSet();
                            adapter = new OleDbDataAdapter(sql.ToString(), conn);
                            adapter.Fill(data);
                            if (data.Tables.Count == 1 && data.Tables[0].Rows.Count == 1 && data.Tables[0].Rows[0][0].ToString() == "TRUE")
                            {
                                DataRow row = data.Tables[0].Rows[0];
                                Dictionary<String, String> dic = new Dictionary<String, String>();
                                dic.Add("磅单单号", row["BillNo"].ToString());
                                dic.Add("车牌号码", PageConfirmCar.Text);
                                dic.Add("预装数量", PageConfirmQty.Text);
                                dic.Add("物料名称", PageConfirmItem.Text);
                                dic.Add("客户名称", OrgName);
                                dic.Add("开票时间", row["PlanTime"].ToString());
                                dic.Add("磁卡编号", row["CardNo"].ToString());
                                dic.Add("开票人", "");
                                if (IsInBag)
                                {
                                    Device.Printer.Print("销售袋装", dic, "");
                                }
                                else
                                {
                                    Device.Printer.Print("销售散装", dic, "");
                                }
                                Device.SendCard();
                                PageConfirm_Message.Text = "办理完毕,请您拿好磁卡,5秒后将自动退出。";
                                PageConfirm_Next.Text = "欢迎再次使用";
                                PageConfirm_Next.Visible = false;
                                PageConfirm_Return.Text = "关闭";
                                Device.Voice("voice8.wav");
                                Device.CountDown = 6;
                            }
                            else
                            {
                                PageConfirm_Message.Text = "执行错误,请联系管理人员:" + data.Tables[0].Rows[0][1].ToString();
                                PageConfirm_Next.Enabled = true;
                                PageConfirm_Next.Text = "确    定";
                            }
                        }
                        catch (Exception ex)
                        {
                            PageConfirm_Message.Text = "数据访问错误：" + ex.Message;
                            PageConfirm_Next.Enabled = true;
                        }
                    }
                    else
                    {
                        Device.ReviceCard();
                        PageConfirm_Message.Text = "出现错误，请重新尝试。";
                        PageConfirm_Next.Text = "重    试";
                        PageConfirm_Next.Enabled = true;
                    }
                }
            }
            catch (Exception ex)
            {
                PageConfirm_Message.Text = "数据访问错误：" + ex.Message;
                PageConfirm_Next.Text = "确    定";
                PageConfirm_Next.Enabled = true;
            }

            ButtonEnd();

        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            Application.DoEvents();
            if (PageSales_NumText.Text == "")
            {
                //Device.Voice("voice7.wav");
            }
        }

        private void ButtonCarNoCHS_Click(object sender, EventArgs e)
        {
            Device.ButtonVoice();
            String value = ((Button)sender).Text;
            if (PageCar_Text.Text.Length == 0)
            {
                PageCar_Text.Text = value;
            }
            else
            {
                if (System.Text.Encoding.GetEncoding("GBK").GetBytes(PageCar_Text.Text.Substring(0, 1)).Length != 1)
                {
                    PageCar_Text.Text = value + PageCar_Text.Text.Substring(1, PageCar_Text.Text.Length - 1);
                }
                else
                {
                    PageCar_Text.Text = value + PageCar_Text.Text;
                }
            }
        }

        private void ButtonCarNoChar_Click(object sender, EventArgs e)
        {
            Device.ButtonVoice();
            if (PageCar_Text.Text.Length < CarLength)
            {
                String value = ((Button)sender).Text;
                PageCar_Text.Text += value;
            }
        }

        private void PageCar_Clear_Click(object sender, EventArgs e)
        {
            Device.ButtonVoice();
            PageCar_Text.Text = String.Empty;
        }

        private void PageCar_Delete_Click(object sender, EventArgs e)
        {
            Device.ButtonVoice();
            if (PageCar_Text.Text.Length > 0)
            {
                PageCar_Text.Text = PageCar_Text.Text.Substring(0, PageCar_Text.Text.Length - 1);
            }
        }

        private void ButtonItemNum_Click(object sender, EventArgs e)
        {
            Device.ButtonVoice();
            String value = ((Button)sender).Text;
            if (Common.CDec(PageSales_PreQty.Text) == 0)
            {
                PageSales_PreQty.Text = value;
            }
            else
            {
                if (PageSales_PreQty.Text.Length < QtyLength)
                {
                    PageSales_PreQty.Text += value;
                }
            }
        }

        private void PageSales_ItemDelete_Click(object sender, EventArgs e)
        {
            Device.ButtonVoice();
            if (PageSales_PreQty.Text.Length > 0)
            {
                PageSales_PreQty.Text = PageSales_PreQty.Text.Substring(0, PageSales_PreQty.Text.Length - 1);
            }
        }

        private void PageSales_ItemClear_Click(object sender, EventArgs e)
        {
            Device.ButtonVoice();
            PageSales_PreQty.Text = String.Empty;
        }

        private void MainTab_Selecting(object sender, TabControlCancelEventArgs e)
        {
            if (e.TabPageIndex == 0 && OrderID != String.Empty)
            {
                e.Cancel = true;
                return;
            }
            if (e.TabPageIndex != 0 && OrderID == String.Empty)
            {
                e.Cancel = true;
                return;
            }
            switch (e.TabPageIndex)
            {
                case 0:
                    Device.Voice("voice7.wav");
                    break;
                case 1:
                    if (PageCar_Panel.Enabled)
                    {
                        PageCar_Message.Text = "请输入您的车牌号。";
                        Device.Voice("voice2.wav");
                    }
                    else
                    {
                        PageCar_Message.Text = "车牌号不能修改，请直接点击下一步选择产品及数量";
                    }
                    break;
                case 2:
                    if (PageSales_ItemList.Enabled && PageSales_QtyPanel.Enabled)
                    {
                        PageSales_PreMessage.Text = "请选择一个产品并且输入本次计划运输的数量。";
                        Device.Voice("voice3.wav");
                    }
                    else if (PageSales_ItemList.Enabled)
                    {
                        PageSales_PreMessage.Text = "请选择一个产品。";
                        Device.Voice("voice4.wav");
                    }
                    else if (PageSales_QtyPanel.Enabled)
                    {
                        PageSales_PreMessage.Text = "请输入本次计划运输的数量。";
                        Device.Voice("voice5.wav");
                    }
                    else
                    {
                        PageSales_PreMessage.Text = "产品和数量不能修改，请直接点击下一步确认出卡。";
                    }
                    break;
                case 3:
                    if (PageCar_Text.Text == String.Empty || PageSales_ItemList.SelectedItem == null || Common.CDec(PageSales_PreQty.Text) == 0)
                    {
                        e.Cancel = true;
                        return;
                    }
                    Device.Voice("voice6.wav");
                    PageConfirmCar.Text = PageCar_Text.Text;
                    PageConfirmItem.Text = ((ListTemp)PageSales_ItemList.SelectedItem).Name;
                    PageConfirmQty.Text = PageSales_PreQty.Text;
                    break;
            }
        }

        private void ButtonCancel_Click(object sender, EventArgs e)
        {
            Device.ButtonVoice();
            Exit();
        }

        private void Exit()
        {
            this.Close();
        }

        private void Clear()
        {
            PageSales_NumText.Text = String.Empty;
            PageCar_Text.Text = String.Empty;
            PageSales_PreQty.Text = String.Empty;
            SalesTab.SelectedTab = TabPageInputBillNo;
        }

        private void PageConfirm_Return_Click(object sender, EventArgs e)
        {
            Device.ButtonVoice();
            if (PageConfirm_Return.Text == "关闭")
            {
                this.Close();
            }
            else
            {
                SalesTab.SelectedTab = TabPageInuputCarNo;
            }
        }

        private void MainForm_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Back:
                    Exit();
                    break;
                case Keys.Escape:
                    Exit();
                    break;
            }


        }

        private void SalesForm_SizeChanged(object sender, EventArgs e)
        {
            //SalesTab.Size = this.Size;
            //PageSales_NumText.Size = new Size(this.Size.Width, PageSales_NumText.Size.Height);
        }

        private void MainTimer_Tick(object sender, EventArgs e)
        {
            if (Device.CountDown > 0)
            {
                Device.CountDown--;
            }
            else if (Device.CountDown == 0)
            {
                this.Close();
            }
            //try
            //{
            //    if (PageSales_NumText.Text == "" && SalesTab.SelectedIndex == 0)
            //    {
            //        String currSerial = Device.ComCardDevice.GetSerial();
            //        if (currSerial != "")
            //        {
            //            if (UseSerial == "")
            //            {
            //                UseSerial = currSerial;
            //                if (Device.ComCardDevice.ReadBlock(KEW.InterFace.Block.Block32) == "销售提货")
            //                {
            //                    PageSales_NumText.Text = Device.ComCardDevice.ReadBlock(KEW.InterFace.Block.Block33); //编号
            //                    PageCar_Text.Text = Device.ComCardDevice.ReadBlock(KEW.InterFace.Block.Block36); //车牌号
            //                    PageSales_PreQty.Text = Common.Qty(Device.ComCardDevice.ReadBlock(KEW.InterFace.Block.Block37)).ToString();
            //                    PreItem = Device.ComCardDevice.ReadBlock(KEW.InterFace.Block.Block38);
            //                    isPswdInputFocus = true;
            //                    CardSerial = UseSerial;
            //                    CardNo = Device.ComCardDevice.ReadBlock(KEW.InterFace.Block.Block34); //磁卡编号
            //                    Device.ButtonVoice();
            //                }
            //            }
            //        }
            //        else
            //        {
            //            UseSerial = "";
            //        }
            //    }
            //}
            //catch (Exception) { }
        }
        private string UseSerial = "";
        

        private List<AccountTemp> AccountList = new List<AccountTemp>();

        private String OrderID = String.Empty;

        

        private void PageSales_ItemList_SelectedIndexChanged(object sender, EventArgs e)
        {
            Device.ButtonVoice();
        }

        private void ButtonTimer_Tick(object sender, EventArgs e)
        {
            if (!IsButtonEnable && DateTime.Now - ButtonOverTime > TimeSpan.FromMilliseconds(800))
            {
                IsButtonEnable = true;
                PageSales_NumNext.Enabled = true;
                PageSales_NumCancel.Enabled = true;
                PageCar_Next.Enabled = true;
                PageCar_Cancel.Enabled = true;
                PageSales_QtyNext.Enabled = true;
                PageSales_QtyCancel.Enabled = true;
                PageConfirm_Next.Enabled = true;
            }
        }

        private void PageSales_NumText_TextChanged(object sender, EventArgs e)
        {
            LabNumberCount.Text = PageSales_NumText.Text.Length.ToString();
        }

        private void PageSales_NumText_Click(object sender, EventArgs e)
        {
            if (!isMustUseCard)
            {
                isPswdInputFocus = false;
            }
        }

        private void PageSales_PswdText_Click(object sender, EventArgs e)
        {
            isPswdInputFocus = true;
        }

    }
}
